package com.pb.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pb.mapper.EmpExprMapper;
import com.pb.mapper.EmpMapper;
import com.pb.pojo.*;
import com.pb.service.EmpLogService;
import com.pb.service.EmpService;
import com.pb.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

@Slf4j
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private EmpExprMapper empExprMapper;

    @Autowired
    private EmpLogService empLogService;

    /*原始分页查询*/
//    @Override
//    public PageResult<Emp> page(Integer page, Integer pageSize) {
//        Long total = empMapper.count();
//
//        List<Emp> rows = empMapper.list((page-1)*pageSize, pageSize);
//
//        return new PageResult<Emp>(total, rows);
//
//    }

//    @Override
//    public PageResult<Emp> page(Integer page, Integer pageSize,
//                                String name, Integer gender,
//                                LocalDate begin,
//                                LocalDate end) {
//        PageHelper.startPage(page,pageSize);
//
//        List<Emp> empList = empMapper.list(name, gender, begin, end);
//
//        Page<Emp> p = (Page<Emp>) empList;
//
//        return new PageResult<Emp>(p.getTotal(), p.getResult());
//
//    }
    @Override
    public PageResult<Emp> page(EmpQueryParam empQueryParam) {
        PageHelper.startPage(empQueryParam.getPage(),empQueryParam.getPageSize());

        List<Emp> empList = empMapper.list(empQueryParam);

        Page<Emp> p = (Page<Emp>) empList;

        return new PageResult<Emp>(p.getTotal(), p.getResult());

    }

    //@Transactional       //开启spring事务 --默认运行异常才回滚
    @Transactional(rollbackFor = {Exception.class}) //全部回滚
    @Override
    public void save(Emp emp){
        try {
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            empMapper.insert(emp);

            List<EmpExpr> exprList = emp.getExprList();
            if(!CollectionUtils.isEmpty(exprList)){
                exprList.forEach(empExpr ->{
                    empExpr.setEmpId(emp.getId());
                } );
                empExprMapper.insertBatch(exprList);
            }
        } finally {
            //记录操作日志
            EmpLog empLog = new EmpLog(null, LocalDateTime.now(), "新增员工："+emp);
            empLogService.insertLog(empLog);
        }

    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void delete(List<Integer> ids) {
        empMapper.deleteByIds(ids);

        empExprMapper.deleteByEmpIds(ids);
    }

    @Override
    public Emp getInfo(Integer id) {
        return empMapper.getById(id);
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);

        empExprMapper.deleteByEmpIds(Arrays.asList(emp.getId()));

        List<EmpExpr> exprList = emp.getExprList();

        if(!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(empExpr ->{
                empExpr.setEmpId(emp.getId());
            } );
            empExprMapper.insertBatch(exprList);
        }


    }

    @Override
    public LoginInfo login(Emp emp) {
        // 1、根据用户名和密码查询员工信息
       Emp e = empMapper.selectByUsernameAndPassword(emp);
        //2、判断员工是否存在
        if(e != null){
            log.info("员工登录：{}",e);

            //生成JWT
            Map<String,Object> claims = new HashMap<>();
            claims.put("id",e.getId());
            claims.put("username",e.getUsername());
            String jwt = JwtUtils.generateJwt(claims);
            return new LoginInfo(e.getId(),e.getUsername(),e.getName(),jwt);
        }
        return null;
    }

    @Override
    public List<Emp> list() {
        return empMapper.findAll();
    }
}
